package com.app.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.app.common.Result;
import com.app.model.DepartmentModel;
import com.app.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/*
部门管理
 */
@RestController
@RequestMapping("/api/depart")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    //添加单位
    @PostMapping("/add")
    public Result<Object> addDepartment(@RequestBody JSONObject json) {
        json.get("id");
        Integer type = (Integer) json.get("type");
        Object name = json.get("unitName");
        Integer sort = (Integer) json.get("sort");
//        Object mobile = json.get("unitMobile");
        return Result.success(departmentService.add(name, sort, type));
    }

    //编辑单位
    @PostMapping("/update")
    public Result<Object> updateDepartment(@RequestBody JSONObject json) {
        Integer id = (Integer) json.get("id");
        Integer type = (Integer) json.get("type");
        String name = (String) json.get("unitName");
        Integer sort = (Integer) json.get("sort");
       // String mobile = (String) json.get("unitMobile");
        return Result.success(departmentService.update(name, sort, id, type));
    }

    //删除单位
    @GetMapping("/delete")
    public Result<Object> deleteDepartmentById(int id) {
        departmentService.deleteDepartmentById(id);
        return Result.success("操作成功！");
    }

    //分页查询
    //查询全部
    @GetMapping("/all")
    public Result<Object> getAll(int page, int limit, String unitName) {
        DepartmentModel model = new DepartmentModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        model.setUnitName(unitName);
        if (unitName == null || unitName.equals("")) {
            return Result.success(departmentService.departmentList(model));
        }
        return Result.success(departmentService.List(model));
    }

    //查询部门类别
    @GetMapping("/getDepartmentClass")
    public Result<Object> getDepartmentClass() {
        return Result.success(departmentService.getDepartmentClass());
    }
}
